﻿namespace WMS6.Web.Outbound
{
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Web.UI;
    using System.Web.UI.HtmlControls;
    using System.Web.UI.WebControls;
    using WMS6.Web;
    using WMS6.Web.OutboundService2Ref;

    public class Vanning : BasePage
    {
        protected HtmlInputButton btnAction;
        protected Button btnDelete;
        protected Button btnEncaseAddDetail;
        protected Button btnEncaseDeleteDetail;
        protected Button btnEncaseNesting;
        protected Button btnEncasePrint;
        protected Button btnGoBack;
        protected Button btnImportData;
        protected Button btnNew;
        protected Button btnSave;
        protected DropDownList ddlOrderType;
        protected DropDownList ddlStatus;
        protected DropDownList ddlVanningType;
        protected HtmlForm F7542;
        protected GridView gvVanningDetail;
        protected HtmlInputHidden hdnActionID;
        protected HtmlInputHidden hdncalendarFlag;
        protected HtmlInputHidden hdnContractID;
        protected HtmlInputHidden hdnDataFormat;
        protected HtmlInputHidden hdnFlag;
        protected HtmlInputHidden hdnFrom;
        protected HtmlInputHidden hdnLineID;
        protected HtmlInputHidden hdnResponse;
        protected HtmlInputHidden hdnVanningID;
        protected HtmlHead Head1;
        protected HtmlInputButton htmlbtnReport;
        protected HtmlImage imgOrderID;
        protected Label lblCreatedBy;
        protected Label lblExternalOrderID;
        protected Label lblMsg;
        protected Label lblOrderID;
        protected Label lblOrderType;
        protected Label lblReamrk;
        protected Label lblSCTotal;
        protected Label lblSCTotalQty;
        protected Label lblStatus;
        protected Label lblTotalHeight;
        protected Label lblTotalHeightText;
        protected Label lblTotalLength;
        protected Label lblTotalLengthText;
        protected Label lblTotalVolume;
        protected Label lblTotalVolumeText;
        protected Label lblTotalWeight;
        protected Label lblTotalWeightText;
        protected Label lblTotalWidth;
        protected Label lblTotalWidthText;
        protected Label lblVanning;
        protected Label lblVanningDate;
        protected Label lblVanningID;
        protected Label lblVanningType;
        protected LinkButton lnkbtnPrintVanning;
        protected HtmlLink mycss;
        protected Label one1;
        protected Label one2;
        protected ScriptManager ScriptManager1;
        private decimal SCTotalQty;
        protected HtmlTableCell tdlnkbtnPrintVanning;
        private decimal TotalHeight;
        private decimal TotalLength;
        private decimal TotalVolume;
        private decimal TotalWeight;
        private decimal TotalWidth;
        protected TextBox txtCreatedBy;
        protected TextBox txtExternalOrderID;
        protected TextBox txtOrderID;
        protected TextBox txtReamrk;
        protected TextBox txtVanningDate;
        protected TextBox txtVanningID;

        protected void btnAddContainerDetail_Click(object sender, EventArgs e)
        {
            if (this.hdnFlag.Value != "1")
            {
                this.lblMsg.Text = "没有保存无法新增容器!";
            }
            else
            {
                base.Response.Redirect(string.Format("VanningDetail.aspx?from={0}&VanningID={1}", this.hdnFrom.Value, this.txtVanningID.Text), true);
            }
        }

        protected void btnDelete_Click(object sender, EventArgs e)
        {
            try
            {
                if (this.hdnFlag.Value != "1")
                {
                    this.lblMsg.Text = "没有保存无法删除!";
                }
                else
                {
                    List<string> vanningID = new List<string> {
                        this.txtVanningID.Text
                    };
                    this.lblMsg.Text = OutboundService2ClientProxy.GetInstance().DeleteVanning(vanningID) + base.GetGlobalMsgResource("F7300_005");
                }
            }
            catch (Exception ex)
            {
                this.lblMsg.Text = base.GetGlobalMsgResource("F7300_006") + ex.Message;
            }
        }

        protected void btnDeleteContainerDetail_Click(object sneder, EventArgs e)
        {
            if (this.hdnFlag.Value != "1")
            {
                this.lblMsg.Text = "没有保存无法删除容器!";
            }
            else
            {
                try
                {
                    List<string> ContainerIDs = base.GetSelectedIDsInGridView(this.gvVanningDetail, "ContainerID");
                    string result = OutboundService2ClientProxy.GetInstance().DeleteVanningContainer(this.hdnVanningID.Value, ContainerIDs);
                    this.lblMsg.Text = base.GetGlobalMsgResource("F7600_011");
                    this.SetVanningInfoForm();
                }
                catch (Exception ex)
                {
                    this.lblMsg.Text = ex.Message;
                }
                finally
                {
                    ScriptManager.RegisterStartupScript(this.Page, base.GetType(), "", "<script language='javascript'>setTab('one',2,2);</script>", false);
                }
            }
        }

        protected void btnGoBack_Click(object sender, EventArgs e)
        {
            base.Response.Redirect(string.Format("VanningList.aspx?from={0}", this.hdnFrom.Value), true);
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            if (this.hdnFlag.Value == "1")
            {
                this.UpdateVanningInfo();
            }
            else
            {
                this.InsertVanningInfo();
            }
        }

        protected void ddlOrderType_TextChanged(object sender, EventArgs e)
        {
            if (this.ddlOrderType.SelectedValue.ToUpper() == "ORDER")
            {
                this.imgOrderID.Attributes.Add("onclick", "selectOrders();");
            }
            else
            {
                this.imgOrderID.Attributes.Add("onclick", "selectBatchOrders();");
            }
            this.txtOrderID.Text = "";
        }

        protected void gvVanningDetail_Init(object sender, EventArgs e)
        {
            base.InitDetailGridView("F7542", this.gvVanningDetail);
        }

        protected void gvVanningDetail_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                int i = this.gvVanningDetail.PageIndex * this.gvVanningDetail.PageSize;
                e.Row.Cells[0].Text = ((e.Row.RowIndex + 1) + i).ToString();
                if (((Label) e.Row.FindControl("lblQty")) != null)
                {
                    this.SCTotalQty += decimal.Parse(((Label) e.Row.FindControl("lblQty")).Text);
                }
                if (((Label) e.Row.FindControl("lblWeight")) != null)
                {
                    this.TotalWeight += decimal.Parse(((Label) e.Row.FindControl("lblWeight")).Text);
                }
                if (((Label) e.Row.FindControl("lblVolume")) != null)
                {
                    this.TotalVolume += decimal.Parse(((Label) e.Row.FindControl("lblVolume")).Text);
                }
                if (((Label) e.Row.FindControl("lblLength")) != null)
                {
                    this.TotalLength += decimal.Parse(((Label) e.Row.FindControl("lblLength")).Text);
                }
                if (((Label) e.Row.FindControl("lbl_Weight")) != null)
                {
                    this.TotalWidth += decimal.Parse(((Label) e.Row.FindControl("lbl_Weight")).Text);
                }
                if (((Label) e.Row.FindControl("lblHeight")) != null)
                {
                    this.TotalHeight += decimal.Parse(((Label) e.Row.FindControl("lblHeight")).Text);
                }
            }
            if (e.Row.RowType == DataControlRowType.Footer)
            {
                this.lblSCTotalQty.Text = base.GetPreferenceDigit(this.SCTotalQty);
                this.lblTotalWeightText.Text = base.GetPreferenceDigit(this.TotalWeight);
                this.lblTotalVolumeText.Text = base.GetPreferenceDigit(this.TotalVolume);
                this.lblTotalLengthText.Text = base.GetPreferenceDigit(this.TotalLength);
                this.lblTotalWidthText.Text = base.GetPreferenceDigit(this.TotalWidth);
                this.lblTotalHeightText.Text = base.GetPreferenceDigit(this.TotalHeight);
            }
        }

        protected void gvVanningDetail_Sorting(object sender, GridViewSortEventArgs e)
        {
            try
            {
                string sortDirection = string.Empty;
                if (base.GridViewSortDirection == SortDirection.Ascending)
                {
                    base.GridViewSortDirection = SortDirection.Descending;
                    sortDirection = "DESC";
                }
                else
                {
                    base.GridViewSortDirection = SortDirection.Ascending;
                    sortDirection = "ASC";
                }
                VanningInfo vanningInfo = base.DataEntityViewState as VanningInfo;
                DataView dv = CADataConverter.ToDataTable(vanningInfo.VanningDetailInfos).DefaultView;
                dv.Sort = e.SortExpression + " " + sortDirection;
                base.BindData2GridView(this.gvVanningDetail, dv);
            }
            catch (Exception ex)
            {
                base.ShowMsgLabel(this.lblMsg, ex.Message);
            }
            finally
            {
                ScriptManager.RegisterStartupScript(this.Page, base.GetType(), "", "<script language='javascript'>setTab('one',2,2);</script>", false);
            }
        }

        private void InsertVanningInfo()
        {
            string vanningID = string.Empty;
            VanningInfo vanningInfo = new VanningInfo {
                VanningID = this.txtVanningID.Text,
                OrderID = this.txtOrderID.Text.Trim(),
                OrderType = this.ddlOrderType.SelectedValue,
                ExternalOrderID = this.txtExternalOrderID.Text.Trim(),
                VanningType = this.ddlVanningType.SelectedValue,
                VanningDate = this.txtVanningDate.Text.Trim(),
                Remark = this.txtReamrk.Text.Trim()
            };
            try
            {
                vanningID = OutboundService2ClientProxy.GetInstance().InsertVanning(vanningInfo);
                this.txtVanningID.Text = vanningID;
                this.hdnVanningID.Value = vanningID;
                this.lblMsg.Text = vanningID + base.GetGlobalMsgResource("F7300_008");
                vanningInfo.VanningID = vanningID;
                base.DataEntityViewState = vanningInfo;
                this.hdnVanningID.Value = this.txtVanningID.Text;
                this.txtVanningID.Enabled = false;
                this.txtOrderID.Enabled = false;
                this.ddlOrderType.Enabled = false;
                this.ddlVanningType.Enabled = false;
                this.imgOrderID.Visible = false;
                this.hdnFlag.Value = "1";
                this.SetVanningInfoForm();
            }
            catch (Exception ex)
            {
                this.lblMsg.Text = ex.Message;
            }
        }

        private void LoadData()
        {
            base.BindData2DDLBySysCode(this.ddlOrderType, "DOC_TYPE");
            base.BindData2DDLBySysCode(this.ddlVanningType, "VanningType");
            base.BindData2DDLBySysCode(this.ddlStatus, "WaveStatus");
            if (!string.IsNullOrEmpty(base.Request.QueryString["ID"]))
            {
                this.txtVanningID.Text = base.Request.QueryString["ID"].ToString();
                this.hdnVanningID.Value = this.txtVanningID.Text;
                this.txtVanningID.Enabled = false;
                this.txtOrderID.Enabled = false;
                this.ddlOrderType.Enabled = false;
                this.ddlVanningType.Enabled = false;
                this.imgOrderID.Visible = false;
                this.SetVanningInfoForm();
                this.hdnFlag.Value = "1";
            }
            else
            {
                this.txtVanningID.Text = "";
                this.hdnVanningID.Value = "";
                this.hdnFlag.Value = "0";
            }
            if (!string.IsNullOrEmpty(base.Request.QueryString["from"]))
            {
                this.hdnFrom.Value = base.Request.QueryString["from"].ToString();
            }
            if (!string.IsNullOrEmpty(base.Request.QueryString["detail"]))
            {
                ScriptManager.RegisterStartupScript(this.Page, base.GetType(), "", "<script language='javascript'>setTab('one',2,2);</script>", false);
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!base.IsPostBack)
            {
                base.InitPage(this.Page);
                this.LoadData();
            }
            this.lblMsg.Text = "";
        }

        private void SetVanningInfoForm()
        {
            try
            {
                string vanningID = this.txtVanningID.Text.Trim();
                if (!string.IsNullOrEmpty(vanningID))
                {
                    VanningInfo vanningInfo = OutboundService2ClientProxy.GetInstance().GetVanningByID(vanningID);
                    base.DataEntityViewState = vanningInfo;
                    if (vanningInfo != null)
                    {
                        this.txtVanningID.Text = vanningInfo.VanningID;
                        this.txtOrderID.Text = vanningInfo.OrderID;
                        this.ddlOrderType.SelectedValue = vanningInfo.OrderType;
                        this.txtExternalOrderID.Text = vanningInfo.ExternalOrderID;
                        this.ddlVanningType.SelectedValue = vanningInfo.VanningType;
                        this.txtCreatedBy.Text = vanningInfo.CreatedBy;
                        this.txtVanningDate.Text = base.GetPreferenceShortDate(vanningInfo.VanningDate);
                        this.txtReamrk.Text = vanningInfo.Remark;
                        base.BindData2GridView(this.gvVanningDetail, vanningInfo.VanningDetailInfos);
                    }
                }
            }
            catch (Exception ex)
            {
                this.lblMsg.Text = ex.Message;
            }
        }

        private void UpdateVanningInfo()
        {
            VanningInfo vanningInfo = new VanningInfo {
                VanningID = this.txtVanningID.Text,
                OrderID = this.txtOrderID.Text.Trim(),
                OrderType = this.ddlOrderType.SelectedValue,
                ExternalOrderID = this.txtExternalOrderID.Text.Trim(),
                VanningType = this.ddlVanningType.SelectedValue,
                VanningDate = this.txtVanningDate.Text.Trim(),
                Remark = this.txtReamrk.Text.Trim()
            };
            try
            {
                this.lblMsg.Text = OutboundService2ClientProxy.GetInstance().UpdateVanning(vanningInfo) + base.GetGlobalMsgResource("F7300_009");
            }
            catch (Exception ex)
            {
                this.lblMsg.Text = ex.Message;
            }
        }
    }
}

